{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 语句类型\n",
    "\n",
    "python 程序语言里面语句较多，基本是一个或多个关键字对应一个语句类型，常用的语句类型如下：\n",
    "\n",
    "* 赋值语句\n",
    "* 判断语句\n",
    "* 循环语句\n",
    "* 解析式语句\n",
    "* 异常捕获语句\n",
    "* 函数定义语句\n",
    "* 函数调用语句\n",
    "* 类定义语句\n",
    "* 类使用语句\n",
    "* 包引入语句\n",
    "\n",
    "这里我们只介绍 *赋值语句* 、 *判断语句* 、 *循环语句*、*解析式语句* ，其他语句适合特定类容相关的，在后面和内容一起介绍。\n",
    "\n",
    "在 python 中语句嵌套组合，形成更复杂的程序结构。"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 赋值语句\n",
    "\n",
    "还记的变量吧，赋值语句主要是针对变量的。每个赋值语句一定包含 `=` 操作符，或者含有 `=` 语义的操作符，例如：`+=`、`*=`、`/=`等。\n",
    "一个赋值语句包含三部分：`=`操作符、左值、右值。在`=`左边的的表达式计算结果称为左值，在`=`右边的表达式计算结果称为右值。\n",
    "\n",
    "* 左值的计算结果一定是一个变量或引用\n",
    "* 右值的计算记过一定是一个对象\n",
    "\n",
    "例如：\n",
    "\n",
    "> `a=1`, 这里 `a` 表示左值，是一个变量；`1` 表示右值，是一个对象\n",
    "> `L[a+1]=5*3`, 这里 `L[a+1]` 表示左值，计算的结果是表示一个引用；`5*3` 表示右值，计算的结果是一个对象\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1"
      ]
     },
     "execution_count": 1,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a=1\n",
    "a"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "6"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a += 5\n",
    "a"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "> 注意：`=` 操作不要求左值变量已经创建，如果没有定义会自行创建。但是在使用 `+=`、`*=`、`/=` 等类似操作符时，要求 **左值既是一个对象，也是一个变量或引用**。因为这类操作符是先计算并获取左值引用的对象，然后使用这个对象和右值计算的对象做运算，最后在赋值给左值的变量或引用。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "ename": "NameError",
     "evalue": "name 'b' is not defined",
     "output_type": "error",
     "traceback": [
      "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[0;31mNameError\u001b[0m                                 Traceback (most recent call last)",
      "\u001b[0;32m<ipython-input-3-be9562e617df>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m()\u001b[0m\n\u001b[1;32m      1\u001b[0m \u001b[0;31m# 由于 变量 b 没有定义，所以在计算 b 的对象时会发生错误\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 2\u001b[0;31m \u001b[0mb\u001b[0m \u001b[0;34m+=\u001b[0m \u001b[0;36m1\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
      "\u001b[0;31mNameError\u001b[0m: name 'b' is not defined"
     ]
    }
   ],
   "source": [
    "# 由于 变量 b 没有定义，所以在计算 b 的对象时会发生错误\n",
    "b += 1"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# if 语句\n",
    "\n",
    "Python条件语句是通过一条或多条语句的执行结果（True或者False）来决定执行的代码块。\n",
    "\n",
    "可以通过下图来简单了解条件语句的执行过程:\n",
    "\n",
    "![](/images/articles/noob-learn-python/if-condition.jpg)\n",
    "\n",
    "Python程序语言指定任何非0和非空（null）值为true，0 或者 null 为false。\n",
    "\n",
    "Python 编程中 if 语句用于控制程序的执行，基本形式为：\n",
    "\n",
    "```\n",
    "if 判断条件：\n",
    "    执行语句……\n",
    "else：\n",
    "    执行语句……\n",
    "```\n",
    "\n",
    "其中\"判断条件\"成立时（非零），则执行后面的语句，而执行内容可以多行，以缩进来区分表示同一范围。\n",
    "\n",
    "else 为可选语句，当需要在条件不成立时执行内容则可以执行相关语句，具体例子如下："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "My name is Lilei!\n"
     ]
    }
   ],
   "source": [
    "name='Lilei'\n",
    "\n",
    "if name=='Lilei':\n",
    "    print(\"My name is Lilei!\")\n",
    "else:\n",
    "    print(\"My name is Hanmeimei!\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "if 语句的判断条件可以用>（大于）、<(小于)、==（等于）、>=（大于等于）、<=（小于等于）来表示其关系。\n",
    "\n",
    "当判断条件为多个值时，可以使用以下形式\n",
    " \n",
    "```\n",
    "if 判断条件1:\n",
    "    执行语句1……\n",
    "elif 判断条件2:\n",
    "    执行语句2……\n",
    "elif 判断条件3:\n",
    "    执行语句3……\n",
    "else:\n",
    "    执行语句4……\n",
    "```"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "My name is Hanmeimei!\n",
      "apple is an ET!\n"
     ]
    }
   ],
   "source": [
    "name='Hanmeimei'\n",
    "\n",
    "if name=='Lilei':\n",
    "    print(\"My name is Lilei!\")\n",
    "elif name=='Hanmeimei':\n",
    "    print(\"My name is Hanmeimei!\")\n",
    "elif name=='dog':\n",
    "    print(\"Sorry, this is a dog!\")\n",
    "else:\n",
    "    print(\"This is an ET!\")\n",
    "    \n",
    "    \n",
    "name='apple'\n",
    "\n",
    "if name=='Lilei':\n",
    "    print(\"My name is Lilei!\")\n",
    "elif name=='Hanmeimei':\n",
    "    print(\"My name is Hanmeimei!\")\n",
    "elif name=='dog':\n",
    "    print(\"Sorry, this is a dog!\")\n",
    "else:\n",
    "    print(name + \" is an ET!\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**判断条件** 是一个计算结果为 **Boolean 值对象** 得表达式。在《Python学习 03：python 数值类型》介绍的的 *比较运算符*、*逻辑运算符*、*成员运算符*、*身份运算符* 这些操作的结果都是 Boolean 值对象，包含后面将介绍的的 *返回 Boolean 值对象的函数调用操作*，只要任意以表达式返回Boolean 值对象都可以作为 `if` 语句的判断条件"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2 在 [1, 2, 3] 中\n",
      "a 不等于 b\n",
      "a 不等于 b 或者 2 不在 [1, 2, 3] 中\n"
     ]
    }
   ],
   "source": [
    "item = 2\n",
    "L = [1,2,3]\n",
    "\n",
    "# 成员运算\n",
    "if item in L:\n",
    "    print(\"%d 在 %s 中\" %(item, str(L))) \n",
    "else:\n",
    "    print(\"%d 没在 %s 中\" %(item, str(L)))\n",
    "    \n",
    "# 比较运算符    \n",
    "a = 1\n",
    "b = 2\n",
    "if a==b:\n",
    "    print(\"a 等于 b\")\n",
    "else:\n",
    "    print(\"a 不等于 b\")\n",
    "\n",
    "# 逻辑运算符\n",
    "if a==b and item in L:\n",
    "    print(\"a 等于 b 同时 %d 在 %s 中\" %(item, str(L)))\n",
    "else:\n",
    "    print(\"a 不等于 b 或者 %d 不在 %s 中\" %(item, str(L)))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 循环语句"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## for 语句\n",
    "\n",
    "Python for循环可以遍历任何序列的项目，如一个列表或者一个字符串。\n",
    "\n",
    "for循环的语法格式如下：\n",
    "\n",
    "```\n",
    "for iterating_var in sequence:\n",
    "   statements(s)\n",
    "```\n",
    "\n",
    "流程图：\n",
    "\n",
    "![](/images/articles/noob-learn-python/python_for_loop.jpg)\n",
    "\n",
    "实例："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "循环字符串：\n",
      "\t当前字符：P\n",
      "\t当前字符：y\n",
      "\t当前字符：t\n",
      "\t当前字符：h\n",
      "\t当前字符：o\n",
      "\t当前字符：n\n",
      "\t当前字符：!\n",
      "循环序列：\n",
      "\t当前元素：4\n",
      "\t当前元素：3\n",
      "\t当前元素：2\n",
      "\t当前元素：1\n",
      "\t当前元素：12\n"
     ]
    }
   ],
   "source": [
    "print(\"循环字符串：\")\n",
    "s = \"Python!\"\n",
    "for letter in s:\n",
    "    print(\"\\t当前字符：\" + letter)\n",
    "    \n",
    "print(\"循环序列：\")\n",
    "l = [4,3,2,1,12]\n",
    "for item in l:\n",
    "    print(\"\\t当前元素：\" + str(item))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "可以以通过序列索引来循环："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "第 1 个水果是 apple\n",
      "第 2 个水果是 orange\n",
      "第 3 个水果是 banana\n"
     ]
    }
   ],
   "source": [
    "fruits = ['apple', 'orange', 'banana']\n",
    "for i in range(len(fruits)):\n",
    "    print(\"第 %d 个水果是 %s\" %(i+1, fruits[i]))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "注意再循环中对于循环变量的赋值操作不会影响到原始序列的值。但是如果序列里面是是可以修改的对象时，通过循环变量的修改操作会同时修改原序列的中的对应值。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[{'a': 'a1'}, {'a': 'a2'}, {'a': 'a3'}]\n",
      "1\n",
      "1\n",
      "1\n",
      "[{'a': 'a1'}, {'a': 'a2'}, {'a': 'a3'}]\n"
     ]
    }
   ],
   "source": [
    "objs = [{'a':'a1'}, {'a':'a2'}, {'a':'a3'}]\n",
    "print(objs)\n",
    "for obj in objs:\n",
    "    obj=1\n",
    "    print(obj)\n",
    "print(objs)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[{'a': 'a1'}, {'a': 'a2'}, {'a': 'a3'}]\n",
      "{'a': 'aaa'}\n",
      "{'a': 'aaa'}\n",
      "{'a': 'aaa'}\n",
      "[{'a': 'aaa'}, {'a': 'aaa'}, {'a': 'aaa'}]\n"
     ]
    }
   ],
   "source": [
    "objs = [{'a':'a1'}, {'a':'a2'}, {'a':'a3'}]\n",
    "print(objs)\n",
    "for obj in objs:\n",
    "    obj['a'] = 'aaa'\n",
    "    print(obj)\n",
    "print(objs)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## while 语句\n",
    "\n",
    "Python 编程中 while 语句用于循环执行程序，即在某条件下，循环执行某段程序，以处理需要重复处理的相同任务。其基本形式为：\n",
    "\n",
    "while 判断条件：\n",
    "    执行语句……\n",
    "执行语句可以是单个语句或语句块。判断条件可以是任何表达式，任何非零、或非空（null）的值均为true。\n",
    "\n",
    "当判断条件假false时，循环结束。\n",
    "\n",
    "执行流程图如下：\n",
    "\n",
    "![](/images/articles/noob-learn-python/python_while_loop.jpg)\n",
    "\n",
    "Gif 演示 Python while 语句执行过程\n",
    "![](images/articles/noob-learn-python/loop-over-python-list-animation.gif)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "The count is:0\n",
      "The count is:1\n",
      "The count is:2\n",
      "The count is:3\n",
      "The count is:4\n",
      "The count is:5\n",
      "The count is:6\n",
      "The count is:7\n",
      "The count is:8\n",
      "Good bye!\n"
     ]
    }
   ],
   "source": [
    "count = 0\n",
    "while count < 9:\n",
    "   print('The count is:' + str(count))\n",
    "   count = count + 1\n",
    " \n",
    "print(\"Good bye!\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "当当前始终为 true 时，将会无限循环"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Enter a number  :\n",
      "You entered: \n",
      "Enter a number  :\n",
      "You entered: \n",
      "Enter a number  :bye\n",
      "You entered: bye\n",
      "Bye Bye!\n"
     ]
    }
   ],
   "source": [
    "s = ''\n",
    "while s!='bye':\n",
    "    s = input(\"Enter a number  :\")\n",
    "    print(\"You entered: \" + s)\n",
    "print(\"Bye Bye!\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## break、continue、pass 语句\n",
    "\n",
    "再循环中常常会遇到提前结束整个循环或提前结束当前循环操作，这个时候就需要用到 `break` 和 `continue`。\n",
    "\n",
    "* break：结束整个循环，剩余的循环次数不再执行\n",
    "* continue：结束本轮循环操作，本轮循环后面的语句不再执行，但是剩余的循环次数将继续执行\n",
    "* pass：空语句，一个占位符，表示这里什么都每一偶"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "正常输出：\n",
      "i = 1\n",
      "i = 2\n",
      "i = 3\n",
      "i = 4\n",
      "i = 5\n",
      "i = 6\n",
      "break 输出：\n",
      "i = 1\n",
      "i = 2\n",
      "break 输出：\n",
      "i = 1\n",
      "i = 2\n",
      "i = 4\n",
      "i = 5\n",
      "i = 6\n"
     ]
    }
   ],
   "source": [
    "l = [1,2,3,4,5,6]\n",
    "\n",
    "print(\"正常输出：\")\n",
    "for i in l:\n",
    "    print(\"i = %d\" % i)\n",
    "    \n",
    "print(\"break 输出：\")\n",
    "for i in l:\n",
    "    \n",
    "    if(i==3):\n",
    "        break  # 这里将结束 i=3 以后的所有输出\n",
    "        \n",
    "    print(\"i = %d\" % i)\n",
    "\n",
    "print(\"break 输出：\")\n",
    "for i in l:\n",
    "    \n",
    "    if(i==3):\n",
    "        continue  # 这里将跳过 i=3 的输出\n",
    "        \n",
    "    print(\"i = %d\" % i)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 语句嵌套\n",
    "\n",
    "从上面的实例可以看到，在 `for` 语句里面包含了 `if` 语句，其实任务语句都可以嵌套使用，语句嵌套使我们可以处理复杂的逻辑和流程。下面通过一个例子说明。\n",
    "\n",
    "> ？输出常见的加法口诀表"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "1+1=2\t\n",
      "1+2=3\t2+2=4\t\n",
      "1+3=4\t2+3=5\t3+3=6\t\n",
      "1+4=5\t2+4=6\t3+4=7\t4+4=8\t\n",
      "1+5=6\t2+5=7\t3+5=8\t4+5=9\t5+5=10\t\n",
      "1+6=7\t2+6=8\t3+6=9\t4+6=10\t5+6=11\t6+6=12\t\n",
      "1+7=8\t2+7=9\t3+7=10\t4+7=11\t5+7=12\t6+7=13\t7+7=14\t\n",
      "1+8=9\t2+8=10\t3+8=11\t4+8=12\t5+8=13\t6+8=14\t7+8=15\t8+8=16\t\n",
      "1+9=10\t2+9=11\t3+9=12\t4+9=13\t5+9=14\t6+9=15\t7+9=16\t8+9=17\t9+9=18\t\n"
     ]
    }
   ],
   "source": [
    "for i in range(10):\n",
    "    for j in range(1, i+1):\n",
    "        print('%d+%d=%d' %(j,i,i+j), end='\\t')\n",
    "    print()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "1+1=2\t\n",
      "1+2=3\t2+2=4\t\n",
      "1+3=4\t2+3=5\t3+3=6\t\n",
      "1+4=5\t2+4=6\t3+4=7\t4+4=8\t\n",
      "1+5=6\t2+5=7\t3+5=8\t4+5=9\t5+5=10\t\n",
      "1+6=7\t2+6=8\t3+6=9\t4+6=10\t5+6=11\t6+6=12\t\n",
      "1+7=8\t2+7=9\t3+7=10\t4+7=11\t5+7=12\t6+7=13\t7+7=14\t\n",
      "1+8=9\t2+8=10\t3+8=11\t4+8=12\t5+8=13\t6+8=14\t7+8=15\t8+8=16\t\n",
      "1+9=10\t2+9=11\t3+9=12\t4+9=13\t5+9=14\t6+9=15\t7+9=16\t8+9=17\t9+9=18\t\n"
     ]
    }
   ],
   "source": [
    "for i in range(10):\n",
    "    for j in range(1, 10):\n",
    "        if(j>i):\n",
    "            break\n",
    "        print('%d+%d=%d' %(j,i,i+j), end='\\t')\n",
    "    print()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 解析式语句\n",
    "\n",
    "解析式语句可以使我们更容易处理列表数据，它的所有能力都可以使用循环语句实现，可以看做循环语句的简略形式。解析式可以任何支持迭代操作的数据结构，包括：列表、元组、自定义支持迭代类型\n",
    "\n",
    "看一下例子：\n",
    "> 将 L 列表中，所有数据都加上 20 "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "循环实现\t\t[21, 22, 23, 24, 25, 26, 27, 28, 29, 30]\n",
      "解析式实现\t[21, 22, 23, 24, 25, 26, 27, 28, 29, 30]\n"
     ]
    }
   ],
   "source": [
    "L = [1,2,3,4,5,6,7,8,9,10]\n",
    "\n",
    "# 循环实现\n",
    "L1=[]\n",
    "for item in L:\n",
    "    L1.append(item+20)\n",
    "print(\"循环实现\\t\\t\",end='');print(L1)\n",
    "\n",
    "# 解析式实现\n",
    "L2=[ item+20 for item in L]\n",
    "print(\"解析式实现\\t\",end='');print(L2)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "可以在解析式里面做数据过滤\n",
    "\n",
    "> 找出 L 中所有的偶数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "循环实现\t\t[2, 4, 6, 8, 10]\n",
      "解析式实现\t[2, 4, 6, 8, 10]\n"
     ]
    }
   ],
   "source": [
    "L = [1,2,3,4,5,6,7,8,9,10]\n",
    "\n",
    "# 循环实现\n",
    "L1=[]\n",
    "for item in L:\n",
    "    if item % 2==0:\n",
    "        L1.append(item)\n",
    "print(\"循环实现\\t\\t\",end='');print(L1)\n",
    "\n",
    "# 解析式实现\n",
    "L2=[ item for item in L if item % 2==0]\n",
    "print(\"解析式实现\\t\",end='');print(L2)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "也可以在解析式里面同时处理多个列表，使其产生多个列表元素组合效果"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[(1, 3), (1, 4), (1, 5), (2, 3), (2, 4), (2, 5), (3, 3), (3, 4), (3, 5)]\n",
      "[(3, 3)]\n"
     ]
    }
   ],
   "source": [
    "L1 = [1,2,3]\n",
    "L2 = [3,4,5]\n",
    "\n",
    "print([(x,y) for x in L1 for y in L2])\n",
    "print([(x,y) for x in L1 for y in L2 if x==y])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "当然解析式也是可以嵌套使用"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[3, 5, 7, 9, 11, 13]\n"
     ]
    }
   ],
   "source": [
    "L = [1,2,3,4,5,6]\n",
    "print([x+1 for x in [x*2 for x in L]])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "使用解析式生成其他数据结构，但是要确保生成的数据结构本省具有可变性，这样就导致元组是不能用解析式生成的。当使用 `(x for x in [1,2,3])` 时，这个其实是 *生成表达式* 语法，会在下次介绍。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1, 4, 9]\n",
      "{'i1': 1, 'i2': 4, 'i3': 9}\n",
      "[16, 25, 36]\n",
      "<generator object <genexpr> at 0x7f7ec8192518>\n"
     ]
    }
   ],
   "source": [
    "L3 = [1,2,3]\n",
    "print([x**2 for x in L3])\n",
    "print({\"i\"+str(x):x**2 for x in L3})\n",
    "\n",
    "print([x**2 for x in (4,5,6)])\n",
    "print((x**2 for x in (4,5,6)))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 试试\n",
    "\n",
    "> ？给定三条线段的长度，判断是否能组成三角形，如果能输出三角形：普通三角形、等腰三角形、等边三角形或直角三角形\n",
    ">\n",
    "> 例如：\n",
    ">\n",
    "> > a=1; b=2; c=3 输出：不是三角形\n",
    "> >\n",
    "> > a=3; b=4; c=5 输出：直角三角形\n",
    "> >\n",
    "> > a=1; b=1; c=1 输出：等边三角形"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
